#!/usr/bin/python
#coding: utf-8

import requests
import re

class GetMail():
    _mail = '' 
    _cookie = ''
    _j_id = 0
    def __init__(self):
        pass

    def _get_email(self, proxy):
        '''								     # потом надо удалить коммент, что бы с прокси работало '''
        req = requests.get('http://10minutemail.com/10MinuteMail/index.html')#, proxies={'http':proxy}, timeout=20)
        print req.headers
        print req.content
        self._cookie = req.cookies['JSESSIONID']

        p = re.compile(r'id=\"javax.faces.ViewState\" value=\"j_id(\d+)\"')
        match = p.search(req.content)
        if match:
            print match.group(1)
            self._j_id = match.group(1)

        p = re.compile('([\w\d]+@[\w.]+) is your temporary e-mail address.')
        match = p.findall(req.content)
        if match:
            return match[0]
        return ''

    def get_mail(self, proxy):
        while(not self._mail): self._mail=self._get_email(proxy)
        print self._mail

    def print_mail(self):
        print self._mail

    def check_mail(self):
        print '\n=:=:=:=:=\n'
        print self._cookie
        print self._j_id
        req = requests.post('http://10minutemail.com/10MinuteMail/index.html', data={'AJAXREQUEST':'j_id3', 'j_id4':'j_id4', 'j_id4:poll':'j_id4:poll', 'javax.faces.ViewState':'j_id'+self._j_id}, headers={}, cookies={'JSESSIONID':self._cookie})
        print req.content
        self.get_content(req.content)
        p = re.compile(r'cid=(\d+)\"')
        match = p.search(req.content)
        if match:
            print match.group(1)
            req = requests.get('http://10minutemail.com/10MinuteMail/error.html?cid=' + match.group(1))
            print req.content
            print
            self.get_content(req.content)    
            
    def get_content(self, data):
        print 'get_content'
        p = re.compile(r'j_id\d+\">(.+?)</a></td><td>(.+?)</td>')
        match = p.search(data, re.M)
        if match:
            print 'FIND!!!'
            print match.group(1)
            print match.group(2)
        else:
            print 'not found!'
